//
//  WholesaleViewController.m
//  LazyCat
//
//  Created by Abner on 16/6/7.
//  Copyright © 2016年 zhanshu. All rights reserved.
//

#import "WholesaleViewController.h"
#import "WholesaleCell.h"
#import "WholeSaleSubVC.h"
#import "WSsearchController.h"
#import "OrderManagerController.h"
#import "MJChiBaoZiHeader.h"
#import "MyCommunity.h"
#define Kapla 0.5
@interface WholesaleViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    //headerV的子控件设置全局变量
    UIImageView *_avatarImgV; //头像
    UILabel *_welcomeLB;
    UILabel *_currentMonthCountLB; //当月进货数量
    UILabel *_todayCountLB;
}
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation WholesaleViewController

-(UIStatusBarStyle)preferredStatusBarStyle {//修改状态栏的颜色
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackColor;
//    [self initNav:@"" doBack:NO];
    self.navigationController.navigationBar.hidden = YES;
    //登录..
    [self loginFirst];
    [self creatTableView];
}

-(void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    self.tableView.backgroundColor = BackColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadPifaCount)];
    
    
    //创建headerview【64+110+65+12】
    UIView *headerV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 251)];
    headerV.backgroundColor= [UIColor whiteColor];
    self.tableView.tableHeaderView = headerV;
    
    //设置headerV的子控件
    UIImageView *bgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 174)];
    bgV.image = [UIImage imageNamed:@"wholesale_bg_icon"];
    [headerV addSubview:bgV];
    
    UILabel *titleLB = [[UILabel alloc]initWithFrame:CGRectMake(40, 20, ScreenWidth-80, 44)];
    titleLB.backgroundColor=[UIColor clearColor];
    titleLB.textAlignment=NSTextAlignmentCenter;
    titleLB.font=[UIFont systemFontOfSize:18];
    titleLB.textColor=[UIColor whiteColor];
    titleLB.text = @"懒猫批发平台";
    [headerV addSubview:titleLB];

    
    
    UIImageView *avatar = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-27, 79, 54, 54)];
    _avatarImgV = avatar;
    avatar.contentMode = UIViewContentModeScaleAspectFit;
    avatar.image = [UIImage imageNamed:@"wholesale_ava"];
    [headerV addSubview:_avatarImgV];
    
    NSString *name = [NSUserDefualManager getValueByKey:UserName];
    UILabel *welcomeLB = [[UILabel alloc]initWithFrame:CGRectMake(0, 145.5, ScreenWidth, 13)];
    _welcomeLB = welcomeLB;
    welcomeLB.textColor = [UIColor whiteColor];
    welcomeLB.textAlignment = NSTextAlignmentCenter;
    welcomeLB.font = [UIFont systemFontOfSize:15];
    welcomeLB.text = [NSString stringWithFormat:@"欢迎您 ,%@",name];
    [headerV addSubview:_welcomeLB];
    
    
    CGFloat midLineW = 1.0;
    CGFloat botViewW = (ScreenWidth - midLineW)/2;
    /**
     上面细线
     */
    UIView *lineV_top = [[UIView alloc]initWithFrame:CGRectMake(0, 174, ScreenWidth, midLineW)];
    lineV_top.backgroundColor = SetColorRGBValue(0xe0e0e0, Kapla);
    [headerV addSubview:lineV_top];
    
    UILabel *currenMonthLB1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 189, botViewW, 15)];
    currenMonthLB1.textColor = SetColorRGBValue(0x757575, 1.0);
    currenMonthLB1.text = @"当月进货(元)";
    currenMonthLB1.textAlignment = NSTextAlignmentCenter;
    currenMonthLB1.font = [UIFont boldSystemFontOfSize:14.0];
    [headerV addSubview:currenMonthLB1];
    
    UILabel *currenMonthLB2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(currenMonthLB1.frame)+10, botViewW, 16)];
    _currentMonthCountLB = currenMonthLB2;
    currenMonthLB2.textColor = SetColorRGBValue(0x000000, 1.0);
    if ([AppData share].MonthBuyCount.length > 0) {
        currenMonthLB2.text = [NSString stringWithFormat:@"%@",[AppData share].MonthBuyCount];
    }else {
        currenMonthLB2.text = @"0.00";
    }
    
    currenMonthLB2.textAlignment = NSTextAlignmentCenter;
    currenMonthLB2.font = [UIFont boldSystemFontOfSize:15.0];
    [headerV addSubview:_currentMonthCountLB];
    
    /**
     中间细线
     */
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/2-midLineW/2, 189, midLineW, 35)];
    lineV.backgroundColor = SetColorRGBValue(0xe0e0e0, Kapla);
    [headerV addSubview:lineV];
    
    UILabel *todayLB1 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2+midLineW/2, 189, botViewW, 15)];
    todayLB1.textColor = SetColorRGBValue(0x757575, 1.0);
    todayLB1.text = @"今日进货(元)";
    todayLB1.textAlignment = NSTextAlignmentCenter;
    todayLB1.font = [UIFont boldSystemFontOfSize:14.0];
    [headerV addSubview:todayLB1];
    
    UILabel *todayLB2 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2+midLineW/2, CGRectGetMaxY(currenMonthLB1.frame)+10, botViewW, 16)];
    _todayCountLB = todayLB2;
    todayLB2.textColor = SetColorRGBValue(0x000000, 1.0);
    if ([AppData share].TodayBuyCount.length > 0) {
    todayLB2.text = [NSString stringWithFormat:@"%@",[AppData share].TodayBuyCount];
    }else {
        todayLB2.text = @"0.00";
    }
    
    todayLB2.textAlignment = NSTextAlignmentCenter;
    todayLB2.font = [UIFont boldSystemFontOfSize:15.0];
    [headerV addSubview:_todayCountLB];
    
    /**
     shang线
     */
    UIView *lineV_bottom = [[UIView alloc]initWithFrame:CGRectMake(0, 239, ScreenWidth, 1)];
    lineV_bottom.backgroundColor = SetColorRGBValue(0xe0e0e0, Kapla);
    [self.tableView addSubview:lineV_bottom];
    
    UIView *V_bottom = [[UIView alloc]initWithFrame:CGRectMake(0, 240, ScreenWidth, 10)];
    V_bottom.backgroundColor = BackColor;
    [self.tableView addSubview:V_bottom];
    /**
     下线
     */
    UIView *lineV_bottom1 = [[UIView alloc]initWithFrame:CGRectMake(0, 250, ScreenWidth, 1)];
    lineV_bottom1.backgroundColor = SetColorRGBValue(0xe0e0e0, Kapla);
    [self.tableView addSubview:lineV_bottom1];
    
    
    
}


- (void)loginFirst {
    NSString *name = [NSUserDefualManager getValueByKey:UserName];
    NSString *psw = [NSUserDefualManager getValueByKey:Psw];
    
    
//    [_loading removeFromSuperview];
//    _loading = [[FeWifiManHub alloc] initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
//    
//    [self.view addSubview:_loading];
//    [_loading show];
    
    //    NSString *cid = [ NSUserDefualManager getValueByKey:@"cid"];
    //    NSLog(@"=-----%@",cid);
    
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSString *cid = [userDefaul objectForKey:@"cid"];
    
    //    NSString *cid = [NSUserDefualManager getValueByKey:@"cid"];
    NSLog(@"=-----%@",cid);
    
    NSString *url = [NSString stringWithFormat:businessLoginUrl,name,psw,cid];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        /**
         *  saleOrLanmao..
         */
        NSString *isLanmao = dic[@"islanmao"];
        if ([isLanmao isEqualToString:@"1"]) {//商家版
            NSString *r = dic[@"r"];
            if ([r isEqualToString:@"T"]) {
                NSDictionary *userMess = dic[@"shopinfo"];
                NSDictionary *userMess111 = dic[@"scoreinfo"];
                
                NSArray *communityArrs = userMess[@"communitys"];
                NSMutableArray *communityNameArs = [NSMutableArray array];
                communityNameArs = [MyCommunity mj_objectArrayWithKeyValuesArray:communityArrs];
                [AppData share].communityArrs = [communityNameArs mutableCopy];
                /***********懒猫掌柜****************/
                [AppData share].doLogin = YES;
                [AppData share].mobile = userMess[@"mobile"];
                [AppData share].username = userMess[@"username"];
                [AppData share].shoplogo = userMess[@"shoplogo"];
                [AppData share].shopname = userMess[@"shopname"];
                [AppData share].contacter = userMess[@"contacter"];
                [AppData share].shortintroduce = userMess[@"shortintroduce"];
                [AppData share].shopnote = userMess[@"shopnote"];
                [AppData share].openingend = userMess[@"openingend"];
                [AppData share].openingstart = userMess[@"openingstart"];
                [AppData share].serveramount = userMess[@"serveramount"];
                [AppData share].minSentamount = userMess[@"minSentamount"];
                [AppData share].privatekey = userMess[@"privatekey"];
                
                
                [AppData share].totalincome = userMess111[@"totalincome"];
                [AppData share].cashbalance = userMess111[@"cashbalance"];
                
                [NSUserDefualManager storageValue:@"1" forKey:@"doLogin"];
                [[AppData share].baseTabVC theShoppingCartCount:userMess[@"shopcart"]];
                
//                [self getData];
            }else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self presentViewController:loginVC animated:YES completion:^{
                    
                }];
                
            }
        }else if ([isLanmao isEqualToString:@"0"]){//批发..
            NSString *r = dic[@"r"];
            if ([r isEqualToString:@"T"]) {
                [AppData share].doLogin = YES;
                
                NSDictionary *infoDic = [dic objectForKey:@"scoreinfo"];
                [AppData share].MonthBuyCount = [infoDic objectForKey:@"totalmonamt"];
                [AppData share].TodayBuyCount = [infoDic objectForKey:@"totaldayamt"];
                [AppData share].username = name;
                [NSUserDefualManager storageValue:@"1" forKey:@"doLogin"];
                if ([AppData share].TodayBuyCount.length > 0) {
                    _todayCountLB.text = [AppData share].TodayBuyCount;
                }else {
                    _todayCountLB.text = @"0.00";
                }
                
                if ([AppData share].MonthBuyCount.length > 0) {
                    _currentMonthCountLB.text = [AppData share].MonthBuyCount;
                }else {
                    _currentMonthCountLB.text = @"0.00";
                }
            }else {
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                [self presentViewController:loginVC animated:YES completion:nil];
            }
        }
    }];
}
//load。。count
- (void)loadPifaCount {
    NSString *url = [NSString stringWithFormat:LoadPifaCount,[AppData share].username,[Utils md5SignStr]];
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
        NSDictionary *dic = result;
        
        if ([dic[@"r"] isEqualToString:@"T"]) {
            NSString *monthStr = dic[@"totalmonamt"];
            NSString *dayStr = dic[@"totaldayamt"];
            _currentMonthCountLB.text = monthStr;
            _todayCountLB.text = dayStr;
            [self.tableView.header endRefreshing];
        }else {
            [self.tableView.header endRefreshing];
        }
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
    
}

#pragma mark - tableView显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"wholesaleCell";
    WholesaleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"WholesaleCell" owner:self options:nil] firstObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.leftImgV.image = [UIImage imageNamed:@"wholesale_pifa_icon"];
        cell.rightImgV.image = [UIImage imageNamed:@"wholesale_dingdan_icon"];
        cell.leftLB.text = @"批发进货";
        cell.rightLB.text = @"订单管理";
        [cell setWholesaleCellBtBlock:^(BOOL isLeft) {
            if (isLeft) {//批发进货
                WholeSaleSubVC *saleVC = [[WholeSaleSubVC alloc]init];
                saleVC.loadUrl = [NSString stringWithFormat:SalePlatM,[AppData share].username];
                saleVC.titleStr = @"批发进货";
                saleVC.isPiFa = YES;
                [self.navigationController pushViewController:saleVC animated:YES];
            }else{//订单管理
//                OrderManagerController *oderVC = [[OrderManagerController alloc]init];
//                [self.navigationController pushViewController:oderVC animated:YES];
                WholeSaleSubVC *saleVC = [[WholeSaleSubVC alloc]init];
                saleVC.loadUrl = [NSString stringWithFormat:OrderManagerM,[AppData share].username];
                saleVC.titleStr = @"订单管理";
                saleVC.isPiFa = YES;
                [self.navigationController pushViewController:saleVC animated:YES];
            }
        }];
    }else if (indexPath.row == 1){
        cell.leftImgV.image = [UIImage imageNamed:@"wholesale_dizhi_icon"];
        cell.rightImgV.image = [UIImage imageNamed:@"wholesale_tuichu_icon"];
        cell.leftLB.text = @"收货地址";
        cell.rightLB.text = @"退出登录";
        
        [cell setWholesaleCellBtBlock:^(BOOL isLeft) {
            if (isLeft) {//收货地址
                WholeSaleSubVC *saleVC = [[WholeSaleSubVC alloc]init];
                
                saleVC.loadUrl = [NSString stringWithFormat:AddressM,[AppData share].username];
                saleVC.titleStr = @"收货地址";
                saleVC.isPiFa = YES;
                [self.navigationController pushViewController:saleVC animated:YES];
            }else{//退出登录
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否退出" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alert.tag = 200;
                [alert show];
            }
        }];
    }
    return cell;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 200) {
        if (buttonIndex == 1) {
            NSString *cid = [ NSUserDefualManager getValueByKey:@"cid"];
            NSString *url = [NSString stringWithFormat:businessLoginOut,cid,[AppData share].username];
            [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
                NSDictionary *dict = result;
                NSString *r = dict[@"r"];
                if ([r isEqualToString:@"T"]) {
                    [[AppData share] clearValue];
                    //                [NSUserDefualManager storageValue:@"" forKey:UserName];
                    [NSUserDefualManager storageValue:@"" forKey:Psw];
                    [AppData share].baseTabVC.redLable.hidden = YES;
                    [NSUserDefualManager storageValue:@"0" forKey:@"doLogin"];
                    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults]; // 蓝牙开关状态
                    [userDefaul setObject:@"off" forKey:@"switch"];
                    LoginViewController *loginVC = [[LoginViewController alloc]init];
                    [self presentViewController:loginVC animated:YES completion:^{
                        
                    }];
                }else {
                    [[MessShowView share] showWithMess:dict[@"m"]];
                }
            }];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
